<template>
  <!-- 对话框(添加 / 修改) -->
  <el-dialog
    :title="title"
    :visible.sync="show"
    width="500px"
    append-to-body
    :before-close="cancel"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="课程总时长" prop="subjectDuration">
        <el-col :span="9">
          <el-input
            type="number"
            v-model.number="form.subjectDuration"
            placeholder="请输入课程总时长"
          />
        </el-col>
        <el-col :span="3">
          <label style="margin-left: 10px">分钟</label>
        </el-col>
      </el-form-item>
      <el-form-item label="关联教材" prop="bookIds">
        <text-book-selector v-model="form.bookIds"></text-book-selector>
        <!-- <el-select v-model="form.bookIds" multiple placeholder="请选择">
          <el-option>
          </el-option>
        </el-select> -->
      </el-form-item>
      <el-form-item label="课程性质" prop="subjectType">
        <el-radio-group v-model="form.subjectType">
          <el-radio
            v-for="dict in this.getDictDatas(
              DICT_TYPE.STUDY_SUBJECT_CONFIG_TYPE
            )"
            :label="parseInt(dict.value)"
            :key="dict.value"
            >{{ dict.label }}</el-radio
          >
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否有效" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio
            v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="parseInt(dict.value)"
            >{{ dict.label }}</el-radio
          >
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { createSubject, updateSubject } from "@/api/study/subject";
import { updateSubjectConfig, getSubjectConfig, getSubjectConfigBySubjectId } from "@/api/study/subjectConfig";
import TextBookSelector from "@/components/CollegeBase/TextBookSelector";
export default {
  name: "SubjectConfig",
  components: { TextBookSelector },
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bookIds: [
          { required: true, message: "关联教材不能为空", trigger: "change" },
        ],
        subjectDuration: [{ required: true, message: "课程时长不能为空", trigger: "blur" }],
        subjectType: [{ required: true, message: "课程类型不能为空", trigger: "change" }],
      },
    };
  },
  created() {

    // this.getDetail();
  },
  props: {
    // 是否显示弹出层
    show: {
      type: Boolean,
      default: false,
    },
    // 显示弹出层标题
    title: {
      type: String,
      default: "",
    },
    id: {

    }

  },
  methods: {
    getDetail() {
      getSubjectConfigBySubjectId(this.id).then((res) => {
        this.form = res.data;
      })
    },
    /** 初始化编辑的数据**/
    initData(rowData) {
      this.reset();
      // if (rowData && rowData.id) {
      //   this.form = rowData;
      //   this.form.datetimerange = [this.form.startTime, this.form.endTime];
      // }
    },
    /** 取消按钮 */
    cancel() {
      this.$emit("update:show", false);
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {

      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return;
        }
        if (this.id) {
          this.form.studySubjectId = this.id
          this.form.id = 0;
          this.form.name = "无";
          updateSubjectConfig(this.form).then((response) => {
            this.$modal.msgSuccess("更新配置成功");
            this.cancel();
            this.$emit("success");
          });
        }
        // if (this.form.id != null) {
        return;
        // }

      });
    },
  },
};
</script>
